// pages/habit/habit.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab_one: true,
    tab_two: false,
    tab_three: false,
    manage: [],
    images: [],
    participate: ['', '', '', ''],

    today: "",
  },

  toDetail: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '../habit_detail/habit_detail?_id=' + that.data.manage[index]._id,
    })
  },

  sign: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    //打卡
    wx.request({
      url: getApp().globalData.my_server + "/minihabit/daka",
      data: {
        _id: that.data.manage[index]._id,
        last_day: that.data.today
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          var manage = that.data.manage
          manage[index].last_day = that.data.today
          manage[index].times = manage[index].times + 1
          that.setData({
            manage: manage
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '打卡失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  generateImages: function(num = 12) {
    const image_list = ["/images/book.png", "/images/english.png", "/images/pen.png", "/images/run.png"]

    var images_temp = []
    for (var i = 0; i < num; i++) {
      var picture = Math.floor(Math.random() * 4)
      images_temp.push(image_list[picture])
    }
    console.log(images_temp)
    this.setData({
      images: images_temp
    })
  },

  create: function() {
    var that = this

    if (that.data.manage.length < 4) {
      wx.redirectTo({
        url: '../habit_new/habit_new',
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '微习惯不要超过4个哦～',
      })
    }

  },

  switch_tab: function(e) {
    console.log(e)
    var tab = e.currentTarget.dataset.tab
    this.setData({
      tab: tab
    })

    if(tab == "2"){
      wx.redirectTo({
        url: '../discovery/discovery',
      })
    }

    if(tab == "3"){
      var that=this
      wx.redirectTo({
        url: '../habit_mine/habit_mine',
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    var day = new Date()
    that.setData({
      today: day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate()
    })

    //获得已创建的微习惯
    wx.login({
      success: function(res_code) {
        if (res_code.code) {

          wx.request({
            url: getApp().globalData.my_server + "/minihabit/minihabits",
            data: {
              code: res_code.code
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.showModal({
                  title: '哎呀～',
                  content: res.data.error_msg,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.error_no == 0) {
                that.generateImages(res.data.minihabits.length)
                that.setData({
                  manage: res.data.minihabits
                })
                getApp().globalData.habit = that.data.manage
                console.log(getApp().globalData.habit)
                console.log(that.data.manage)
              }
            },
            fail: function(res) {
              wx.showModal({
                title: '哎呀～',
                content: '获得已创建的微习惯失败',
                success: function(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function(res) {

            }
          })


        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})